package DensityObjects
{
	import FeedbackObjects.MessageBox;
	
	import MyDisplayObjects.MyButton;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class FlickingPanel extends MovieClip
	{
		public static const GROUND_COLOR:uint = 0xFFCCAA;
		
		private var _objects:Array;
		private var _densityButton:MyButton;
		private var _showingDensity:Boolean = false;
		private var _allowShowDensity:Boolean;
		
		public function FlickingPanel(width:Number, height:Number, allowShowDensity:Boolean = true)
		{
			this.graphics.beginFill(GROUND_COLOR,1);
			this.graphics.drawRect(0, 0, width, height);
			this.graphics.endFill();
			_allowShowDensity = allowShowDensity;
			this.addEventListener(Event.ADDED, handleAdded);
		}
		private function handleAdded(event:Event):void
		{
			this.removeEventListener(Event.ADDED, handleAdded);
			// add a "show
			if (_allowShowDensity)
			{
				_densityButton = new MyButton (this.width/4, this.width/8, this.width/16, this.width/16, "Show densities", 0xDDDDFF, 0, true, false, MyButton.AUTO_COLOR, "", MyButton.AUTO_COLOR, MyButton.AUTO_COLOR, "", MyButton.AUTO_COLOR, MyButton.AUTO_COLOR, "Hide densities");
				_densityButton.addEventListener(MouseEvent.CLICK, handleClickDensityButton);
				_densityButton.x = this.width - _densityButton.width;
				_densityButton.y = this.height - _densityButton.height;
				this.addChild(_densityButton);
			}
			
			_objects = new Array();
		}
		
		/** If user clicks on density button go through each flicable mc and show density */
		private function handleClickDensityButton (event:MouseEvent):void
		{
			var fmc:FlickableMC;
			if (_showingDensity)
			{
				for each (fmc in _objects)
				{
					fmc.hideDensity();
				}
				_showingDensity = false;
			} else
			{
				for each (fmc in _objects)
				{
					fmc.showDensity();
				}
				_showingDensity = true;
			}	
		}
		
		/** This function is used to add a new flickable object (or shape) to the panel, at specified location */
		public function addFlickableMC (type:String, x:Number, y:Number, mass:Number, volume:Number=0, friction:Number=0)
		{
			if (type == "RectPrism")
			{
				var fmc:FlickableMC = new FlickableShape ("RectPrism", 2, 2, 2, mass, friction);
				fmc.x = x; fmc.y = y;
				this.addChild(fmc);
				_objects.push(fmc);	
			}
		}
		
		public function addNewMessage (_message:String, initx:Number, inity:Number, type:String, id:String="", MAX_WIDTH:Number=Number.MAX_VALUE, MAX_HEIGHT:Number=Number.MAX_VALUE)
		{
			var m:MessageBox = new MessageBox(_message, initx, inity, type, id, MAX_WIDTH, MAX_HEIGHT);
			this.addChild(m);			
		}
	}
}